'--------------------------------------------------------'
' Description:
'   This script intends for active EEWPF.EECom object.
'	Please feel free to use and modify it in your way.
' Author: lotus
'--------------------------------------------------------'
Option Explicit

CALL CallEEWPF

'----------------------
' active EECom
'----------------------
Function CallEEWPF()
	dim myObj
	Dim viaCount
	Dim errMsg

    Dim pcbDoc
    Set pcbDoc = RetainActiveDoc()
	
	'Please ensure you have register the EECom component DLL into your Windows OS.
	'Note: Install the correspondent X86 or X64 version of EECom which is same with
	'your local XPEdition application's version.
	Set myObj = CreateObject("EEWPF.EECom")
	myObj.RunWithDoc(pcbDoc)

	Set myObj = Nothing
	Set pcbDoc = Nothing
End Function

'----------------------
' get ActiveDocument
'----------------------
Function RetainActiveDoc()
' Get the application object 
    Dim pcbApp
    Set pcbApp = Application

    ' Get the active document 
    Dim pcbDoc
    Set pcbDoc = pcbApp.ActiveDocument

    ' License the document 
    ValidateServer(pcbDoc)

    Set RetainActiveDoc = pcbDoc
End Function

' Server validation function
Function ValidateServer(docObj)
  
  Dim keyInt
  Dim licenseTokenInt
  Dim licenseServerObj

  ' Ask Expedition’s document for the key
  keyInt = docObj.Validate(0)

  ' Get license server
  Set licenseServerObj = CreateObject("MGCPCBAutomationLicensing.Application")

  ' Ask the license server for the license token
  licenseTokenInt = licenseServerObj.GetToken(keyInt)

  ' Release license server
  Set licenseServerObj = nothing

  ' Turn off error messages (validate may fail if the token is incorrect)
  On Error Resume Next
  Err.Clear

  ' Ask the document to validate the license token
  docObj.Validate(licenseTokenInt)
  If Err Then
	  ValidateServer = 0    
  Else
	  ValidateServer = 1
  End If

End Function
